package n20;

public class Main2 {
    public boolean isValid(String s) {
        byte[] bytes = s.getBytes();
        byte[] lefts = new byte[bytes.length];
        int temp = 0;
        for (byte b : bytes) {
            if (b == 40 || b == 91 || b == 123) {
                lefts[temp++] = b;
            } else {
                if (temp==0) return false;
                if ((b == 41 && lefts[--temp] != 40) || (b == 93 && lefts[--temp] != 91)
                        || (b == 125 && lefts[--temp] != 123)) return false;
            }
        }
        return temp==0;
    }

    public static void main(String[] args) {
        String[] strs = {"()","()[]{}", "(]", "([)]", "{[]}"}; // true, true, false, false, true
        Main2 main = new Main2();
        for (String str : strs) {
            System.out.println(main.isValid(str));
        }
    }
}
